package com.airbnb.epoxy.models;

import butterknife.BindComponent;
import com.airbnb.epoxy.EpoxyAttribute;
import com.airbnb.epoxy.EpoxyModelClass;
import com.airbnb.epoxy.EpoxyModelWithHolder;
import com.airbnb.epoxy.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import static com.airbnb.epoxy.EpoxyAttribute.Option.DoNotHash;

@EpoxyModelClass(layout = ResourceTable.Layout_model_text)
public abstract class TextBindingModel extends EpoxyModelWithHolder<TextBindingModel.TextHolder> {
  @EpoxyAttribute /*@ElementRes */String textRes;
  @EpoxyAttribute(DoNotHash)
  Component.KeyEventListener listener;
  @Override
  public void bind(TextHolder holder) {
    if(holder.text != null) {
      holder.text.setText(textRes);
      holder.text.setKeyEventListener(listener);
    }
  }

  @Override
  public void unbind(TextHolder holder) {
    // Release resources and don't leak listeners as this view goes back to the view pool
    if(holder.text != null) {
      holder.text.setText(null);
      holder.text.setKeyEventListener(null);
    }
  }

  public static class TextHolder extends BaseEpoxyHolder {
    @BindComponent(ResourceTable.Id_text)
    Text text;
  }
}
